{include file="common/head"/}
<div class="admin-main layui-anim layui-anim-upbit">
    <fieldset class="layui-elem-field layui-field-title">
        <legend>网关{:lang('list')}</legend>
    </fieldset>
    <div class="demoTable">
        <div class="layui-inline">
            <input class="layui-input" name="key" id="key" placeholder="输入网关编号或名称 大棚名称">
        </div>
        <button class="layui-btn" id="search" data-type="reload">搜索</button>
        <a href="javascript:location.replace(location.href);" class="layui-btn">刷新</a>
        <a  class="layui-btn"   style="margin-right: 0px;float: right;" id="add_dev" data-type="reload" onclick="add_gateway()"  >添加网关</a>
        <div style="clear: both;"></div>
    </div>
    <table class="layui-table" id="list" lay-filter="list"></table>
</div>
<script type="text/html" id="is_open">
    <input type="checkbox" name="is_open" value="{{d.id}}" lay-skin="switch" lay-text="开启|禁用" lay-filter="is_open" {{ d.is_open == 1 ? 'checked' : '' }}>
</script>
<script type="text/html" id="operation">
    <a class="layui-btn layui-btn-normal layui-btn-xs" onclick="devEdit('{{d.id}}')" >编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>
{include file="common/foot"/}
<script src="/static/common/js/jquery.2.1.1.min.js"></script>
<script>
    //添加网关
    function  add_gateway() {
        layer.open({
            type: 2,
            title: '添加网关',
            shade: 0.01,
            shadeClose: true,
            scrollbar: false,
            area: ['800px', '400px'],
            anim: 5,
            content: "{:url('addGateway')}"
        });
    }


    /**
     * 网关编辑
     */
    function  devEdit(id) {
        layer.open({
            type: 2,
            title: '网关编辑',
            shade: 0.01,
            shadeClose: true,
            scrollbar: false,
            area: ['800px', '400px'],
            anim: 5,
            content: "{:url('gatewayEdit')}?id=" + id
        });
    }
</script>
<script>
    layui.use(['table','form'], function() {
        // 列表
        var table = layui.table,form = layui.form, $ = layui.jquery;
        var tableIn = table.render({
            id: 'user',
            elem: '#list',
            url: '{:url("gateway")}',
            method: 'post',
            page: {
                curr: location.hash.replace('#!gateway=', '') 
                , hash: 'gateway'
            },
            cols: [[
                {field:'id', title: '序号', width:80,fixed: true,align:'center'},
                {field:'gateway_id', title: '网关编号', width:150,align:'center'},
                {field:'gateway_name', title: '网关名称', width:300,align:'center'}
                ,{field:'house_name', title: '大棚名称',width:300,align:'center'}
                ,{field:'is_bad', title: '是否故障',width:300,align:'center'}
                ,{field:'is_open', title: '{:lang("status")}',width:300,toolbar: '#is_open', align:'center'},
                {field: 'is_del', align:'center', title: '操作', toolbar: ' #operation '}
            ]],
            limit: 10 //每页默认显示的数量
        });

        //状态开关
        form.on('switch(is_open)', function(obj){
            loading =layer.load(1, {shade: [0.1,'#fff']});
            var id = this.value;
            var is_open = obj.elem.checked===true?1:2;
            $.post('{:url("gatewayState")}',{'id':id ,'is_open':is_open},function (res) {
                layer.close(loading);
                console.log(res);
                if (res.code===1) {
                    layer.msg(res.msg,{time:1000,icon:1});
                    tableIn.reload();
                }else{
                    layer.msg(res.msg,{time:1000,icon:2});
                    return false;
                }
            })
        });
        table.on('tool(list)',function (obj) {
            var data = obj.data;
            //操作   删除
            if(obj.event==='del'){
                layer.confirm('确定删除吗？',function (index) {
                    $.post("{:url('gatewayDel')}",{id:data.id},function (res) {
                        console.log(res);
                        if(res.code===1){
                            layer.msg(res.msg,{time:1000,icon:1});
                            tableIn.reload();
                        }else{
                            layer.msg(res.msg,{time:1000,icon:2});
                        }
                    });
                    layer.close(index);
                })
            }

        });
        //搜索
        $('#search').on('click', function() {
            var key = $('#key').val();
            if($.trim(key)==='') {
                layer.msg('{:lang("pleaseEnter")}关键字！',{icon:0});
                return;
            }
            tableIn.reload({
                where: {key: key}
            });
        });

    });

</script>
</body>
</html>
